package fr.utbm.tx52.reception;

import fr.utbm.tx52.R;
import fr.utbm.tx52.contactsManagement.Contact;
import fr.utbm.tx52.dataManagement.DataManager;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ViewPhoto extends Activity implements OnClickListener {

	/**
	 * Bouton de lecture / stop
	 */
	private Button btnPlay;
	
	/**
	 * Lecteur audio
	 */
	private MediaPlayer mp = new MediaPlayer();
	
	/**
	 * Chemin vers le fichier audio
	 */
	private String audioFilePath;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seefile);
        
        PullService.cancelNotification(this);
        
        String sendBy = getIntent().getStringExtra("sendby");
        String imagePath = getIntent().getStringExtra("photopath");
        audioFilePath = getIntent().getStringExtra("soundpath");
        String comment = getIntent().getStringExtra("comment");
        
        setTitle("Envoyé par " + Contact.getContactsByPhone(this, sendBy).getName());
        
        TextView t_comment = (TextView)findViewById(R.id.commentview);
        t_comment.setText(comment);
        
        Log.d(getClass().getName(), "Sound = " + audioFilePath);
        btnPlay = (Button) findViewById(R.id.btn_file_play_stop);
        if ( audioFilePath.equals("null") ) {
        	btnPlay.setVisibility(View.GONE);
        }
        btnPlay.setOnClickListener(this);
        
        ImageView iv = (ImageView)findViewById(R.id.shared_image);
        Bitmap b = BitmapFactory.decodeFile(DataManager.DATA_PATH + imagePath);
        if ( b == null ) {
        	Log.d(getClass().getName(), "Impossible de charger la photo !");
        }
        iv.setImageBitmap(b);
        
    }


	public void onClick(View v) {
		switch(v.getId()) {
		
		case R.id.btn_file_play_stop :
			
			if (mp.isPlaying()) {
				mp.stop();
				mp.reset();
				btnPlay.setText("Lire le commentaire");
			} else {
			
				try {
					mp.setDataSource(DataManager.DATA_PATH + audioFilePath);
					mp.prepare();
				} catch (Exception e) {
					e.printStackTrace();
				}
				mp.start();
				btnPlay.setText("Stop");

				mp.setOnCompletionListener(new OnCompletionListener() {
					public void onCompletion(MediaPlayer mp) {
						mp.stop();
						mp.reset();
						btnPlay.setText("Lire le commentaire");	
					}
				});

			}
				
			break;
		}
		
	}
	
}
